package com.yyh.synctool.client.controller;

import com.yyh.result.JsonResult;
import com.yyh.synctool.client.component.Setting;
import com.yyh.synctool.client.pojo.dto.TaskTypeDTO;
import com.yyh.synctool.client.pojo.vo.TaskParam;
import com.yyh.synctool.client.tool.ConfigKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author xuan
 * @date 2021/12/13
 */
@RestController
@RequestMapping("/task-setting")
public class TaskSettingController
{
    private Setting setting;

    @Autowired
    public TaskSettingController(Setting setting) {
        this.setting = setting;
    }

    @GetMapping("/task-type")
    public JsonResult getTaskType() {
        ArrayList<TaskTypeDTO> data = new ArrayList<>();
        data.add(new TaskTypeDTO(ConfigKey.TASK_STOCK, "库存"));
        data.add(new TaskTypeDTO(ConfigKey.TASK_PRICE, "价格"));
        data.add(new TaskTypeDTO(ConfigKey.TASK_PRICE, "商品批号效期"));
        data.add(new TaskTypeDTO(ConfigKey.TASK_PRICE, "商品信息"));
        data.add(new TaskTypeDTO(ConfigKey.TASK_PRICE, "订单汇总"));
        data.add(new TaskTypeDTO(ConfigKey.TASK_PRICE, "订单明细"));

        return JsonResult.getSuccess(data);
    }

    @GetMapping("/showSql")
    public JsonResult showSql(@RequestParam("value") String value) {
        String taskSql = setting.getTaskSql(value);
        HashMap<String, String> data = new HashMap<>();
        data.put("sql", taskSql);

        return JsonResult.getSuccess(data);
    }

    @PostMapping("/save")
    public JsonResult saveSetting(@RequestBody TaskParam taskParam) {
        setting.setTaskSql(taskParam.getValue(), taskParam.getSql());
        setting.saveTaskConfig();

        return JsonResult.getSuccess("保存成功");
    }
}
